package pcjavanet.mobilesell.service;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;  
import java.util.Iterator; 
import java.util.Map;      
import pcjavanet.mobilesell.model.Shop;
import pcjavanet.mobilesell.model.ShopExample;
import pcjavanet.mobilesell.model.Color;
import pcjavanet.mobilesell.model.ColorExample;
import pcjavanet.mobilesell.model.Configuration;
import pcjavanet.mobilesell.model.ConfigurationExample;
import pcjavanet.mobilesell.model.Unit;
import pcjavanet.mobilesell.model.UnitExample;
import pcjavanet.mobilesell.model.Purchase; 
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.PurchaseExample;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;
import pcjavanet.mobilesell.persistence.ShopMapper;
import pcjavanet.mobilesell.persistence.ColorMapper;
import pcjavanet.mobilesell.persistence.ConfigurationMapper;
import pcjavanet.mobilesell.persistence.UnitMapper;
import pcjavanet.mobilesell.persistence.PurchaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class PurchaseService {


  @Autowired
  private  PurchaseMapper purchaseMapper;
  @Autowired
  private  ShopMapper shopMapper;
  @Autowired
  private  ColorMapper colorMapper;
  @Autowired
  private  ConfigurationMapper configurationMapper;
  @Autowired
  private  UnitMapper unitMapper;


  public int  insert( Purchase purchase ) throws Exception{
	return purchaseMapper.insert (purchase);
  }


  public Purchase  selectByPrimaryKey( Purchase  bean ) {
 	int dbid = bean.getDbid();
	Purchase purchase = purchaseMapper.selectByPrimaryKey( dbid );
	Shop shop = shopMapper.selectByPrimaryKey( purchase.getRefShopDbidshopDbid() );
	purchase.setShop(shop) ;
	Color color = colorMapper.selectByPrimaryKey( purchase.getRefColorDbid() );
	purchase.setColor(color) ;
	Configuration configuration = configurationMapper.selectByPrimaryKey( purchase.getRefConfigurationDbid() );
	purchase.setConfiguration(configuration) ;
	Unit unit = unitMapper.selectByPrimaryKey( purchase.getRefUnitDbid() );
	purchase.setUnit(unit) ;
	return purchase ;
  }


  public int  updateByPrimaryKeySelective( Purchase purchase ) throws Exception{
	return purchaseMapper.updateByPrimaryKeySelective( purchase );
  }


  public int  deleteByPrimaryKey( Purchase  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return purchaseMapper.deleteByPrimaryKey( dbid );
  }


  public List<Purchase>  selectByExample( Purchase purchase ) throws Exception{
    PurchaseExample example = new PurchaseExample();
	return purchaseMapper.selectByExample( example );
  }

  public List<Purchase>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    PurchaseExample example = new PurchaseExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<Purchase> ls=purchaseMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> shopIdmap = new HashMap<Integer,Integer>();
  		for(Purchase  m : ls){
  			m.setShop(new Shop ()) ; 
  			if ( m.getRefShopDbidshopDbid() != null ) 
  				shopIdmap.put(m.getRefShopDbidshopDbid(), m.getRefShopDbidshopDbid());
  		};
  		List<Integer> refShopDbidshopDbids = new ArrayList<Integer>();
  		Iterator<Integer> refShopDbidshopDbidit = shopIdmap.keySet().iterator(); 
  		while ( refShopDbidshopDbidit.hasNext() ){
  			refShopDbidshopDbids.add( refShopDbidshopDbidit.next() );
  		}
  		ShopExample shopExample = new ShopExample();
  		if ( refShopDbidshopDbids.size() > 0 ) 
  			shopExample.createCriteria().andDbidIn( refShopDbidshopDbids );
  		List<Shop> shops = shopMapper.selectByExample( shopExample);
  		for(Purchase m : ls ){
  			for(Shop c : shops ){
  				if ( m.getRefShopDbidshopDbid() !=null && c.getDbid() !=null && m.getRefShopDbidshopDbid().intValue() == c.getDbid().intValue() ) {
  					m.setShop( c );
  				}
  			}
  		}
  		Map<Integer,Integer> colorIdmap = new HashMap<Integer,Integer>();
  		for(Purchase  m : ls){
  			m.setColor(new Color ()) ; 
  			if ( m.getRefColorDbid() != null ) 
  				colorIdmap.put(m.getRefColorDbid(), m.getRefColorDbid());
  		};
  		List<Integer> refColorDbids = new ArrayList<Integer>();
  		Iterator<Integer> refColorDbidit = colorIdmap.keySet().iterator(); 
  		while ( refColorDbidit.hasNext() ){
  			refColorDbids.add( refColorDbidit.next() );
  		}
  		ColorExample colorExample = new ColorExample();
  		if ( refColorDbids.size() > 0 ) 
  			colorExample.createCriteria().andDbidIn( refColorDbids );
  		List<Color> colors = colorMapper.selectByExample( colorExample);
  		for(Purchase m : ls ){
  			for(Color c : colors ){
  				if ( m.getRefColorDbid() !=null && c.getDbid() !=null && m.getRefColorDbid().intValue() == c.getDbid().intValue() ) {
  					m.setColor( c );
  				}
  			}
  		}
  		Map<Integer,Integer> configurationIdmap = new HashMap<Integer,Integer>();
  		for(Purchase  m : ls){
  			m.setConfiguration(new Configuration ()) ; 
  			if ( m.getRefConfigurationDbid() != null ) 
  				configurationIdmap.put(m.getRefConfigurationDbid(), m.getRefConfigurationDbid());
  		};
  		List<Integer> refConfigurationDbids = new ArrayList<Integer>();
  		Iterator<Integer> refConfigurationDbidit = configurationIdmap.keySet().iterator(); 
  		while ( refConfigurationDbidit.hasNext() ){
  			refConfigurationDbids.add( refConfigurationDbidit.next() );
  		}
  		ConfigurationExample configurationExample = new ConfigurationExample();
  		if ( refConfigurationDbids.size() > 0 ) 
  			configurationExample.createCriteria().andDbidIn( refConfigurationDbids );
  		List<Configuration> configurations = configurationMapper.selectByExample( configurationExample);
  		for(Purchase m : ls ){
  			for(Configuration c : configurations ){
  				if ( m.getRefConfigurationDbid() !=null && c.getDbid() !=null && m.getRefConfigurationDbid().intValue() == c.getDbid().intValue() ) {
  					m.setConfiguration( c );
  				}
  			}
  		}
  		Map<Integer,Integer> unitIdmap = new HashMap<Integer,Integer>();
  		for(Purchase  m : ls){
  			m.setUnit(new Unit ()) ; 
  			if ( m.getRefUnitDbid() != null ) 
  				unitIdmap.put(m.getRefUnitDbid(), m.getRefUnitDbid());
  		};
  		List<Integer> refUnitDbids = new ArrayList<Integer>();
  		Iterator<Integer> refUnitDbidit = unitIdmap.keySet().iterator(); 
  		while ( refUnitDbidit.hasNext() ){
  			refUnitDbids.add( refUnitDbidit.next() );
  		}
  		UnitExample unitExample = new UnitExample();
  		if ( refUnitDbids.size() > 0 ) 
  			unitExample.createCriteria().andDbidIn( refUnitDbids );
  		List<Unit> units = unitMapper.selectByExample( unitExample);
  		for(Purchase m : ls ){
  			for(Unit c : units ){
  				if ( m.getRefUnitDbid() !=null && c.getDbid() !=null && m.getRefUnitDbid().intValue() == c.getDbid().intValue() ) {
  					m.setUnit( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    PurchaseExample example = new PurchaseExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return purchaseMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Purchase purchase , PageBean pageBean ){
    PurchaseExample example = new PurchaseExample();
	return purchaseMapper.countByExample( example );
  }
  public List<Purchase> selectByExamplePage( Purchase purchase , PageBean pageBean ){
  PurchaseExample example = new PurchaseExample();
	return purchaseMapper.selectByExamplePage( example , pageBean );
  }

}
